<template>
  <div class="register-container">
    <el-card class="login-card">
      <template #header>
        <h2 class="title">用户注册</h2>
      </template>
      <el-form :model="form" ref="formRef" label-width="100px" label-position="top" class="login-form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="姓名" prop="cardName">
          <el-input v-model="form.cardName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="primary" @click="navigateToLogin">返回登录</el-button>
            <el-button type="success" @click="handleRegister">注册</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>


<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const form = ref({
  username: '',
  password: '',
  cardName: '',
});

const router = useRouter();

const handleRegister = async () => {
  try {
    const { username, password, cardName } = form.value;
    const response = await http.post('/user/register', {
      username,
      password,
      cardName,
    });
    if (response && response.code === 200) {
      ElMessage.success(response.data || '注册成功');
      router.push('/login');
    } else {
      ElMessage.error(response.data || '注册失败');
    }
  } catch (error) {
    console.error('请求错误:', error);
    ElMessage.error('网络或服务器错误，请稍后再试');
  }
};

const navigateToLogin = () => {
  router.push('/login');
};
</script>

 <style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f4f6f9;
  background-image: url('../assets/images/background.jpg'); /* 添加背景图片 */
  background-size: cover; /* 使背景图片覆盖整个容器 */
  background-position: center; /* 将背景图片居中 */
  background-repeat: no-repeat; /* 防止背景图片重复 */
  min-height: 100vh; /* 确保背景图片至少覆盖整个视口高度 */
}

/* 卡片样式 */
.login-card {
  width: 420px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
}

/* 标题样式 */
.title {
  text-align: center;
  color: #409EFF;
  font-size: 26px;
  letter-spacing: 2px;
  margin: 15px 0;
}

/* 表单统一宽度 */
.login-form .el-form-item__content {
  width: 100%;
}

/* 表单项间距 */
.el-form-item {
  margin-bottom: 20px;
}

/* 输入框样式 */
.el-input__inner {
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: box-shadow 0.3s;
}

.el-input__inner:focus {
  border-color: #409EFF;
  box-shadow: 0 0 8px rgba(64, 158, 255, 0.3);
}

/* 按钮组样式 */
.el-button-group .el-button {
  padding: 12px 73px;
}

.el-button-group .el-button--text {
  color: #409EFF;
}

.el-button-group .el-button--text:hover {
  color: #66b1ff;
}
</style>

  